package com.eqdd.yiqidian.ui.select;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.arlib.floatingsearchview.FloatingSearchView;
import com.arlib.floatingsearchview.suggestions.model.SearchSuggestion;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.SelectTeacherActivityCustom;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.HttpResultRows;
import com.eqdd.yiqidian.bean.rebuild.TeacherListBean;
import com.eqdd.yiqidian.bean.rebuild.TeacherListBean;
import com.eqdd.yiqidian.bean.rebuild.ZhangPageResultBean;
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource;
import com.eqdd.yiqidian.helper.mvchelper.Rx2DataSource;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.JsonConverter;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.widget.FlowLayoutManager;
import com.lzy.okgo.OkGo;
import com.lzy.okrx2.adapter.FlowableBody;
import com.shizhefei.mvc.MVCCoolHelper;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import io.reactivex.Flowable;

/**
 * @author吕志豪 .
 * @date 18-1-26  下午1:44.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class SelectTeacherActivity extends BaseActivity {

    private SelectTeacherActivityCustom dataBinding;
    private int pageNum;
    private String researchField;
    private String para;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_select_teacher);
        initTopTitleBar(View.VISIBLE, "讲师搜索");
    }

    @Override
    public void onResume() {
        dataBinding.floatingSearchView.setQueryTextSize(18);
        super.onResume();
    }

    @Override
    public void initData() {
        researchField = getIntent().getStringExtra("ResearchField");
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        SlimAdapterEx<TeacherListBean> slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_106, new SlimInjector<TeacherListBean>() {
            @Override
            public void onInject(TeacherListBean data, IViewInjector injector) {
                injector.imageCircle(R.id.iv_head, data.getHeadimage())
                        .text(R.id.tv_name, data.getRealname())
                        .text(R.id.tv_address, data.getCity());
                String[] split = data.getResearchField().split(",");
                List<String> strings = Arrays.asList(split);
                RecyclerView recyclerView = (RecyclerView) injector.findViewById(R.id.recyclerView);
                recyclerView.setLayoutManager(new FlowLayoutManager());
                SlimAdapterEx slimAdapterExFlow = SlimAdapterEx.create().register(R.layout.list_item_107, new SlimInjector<String>() {
                    @Override
                    public void onInject(String data, IViewInjector injector) {
                        injector.text(R.id.bt_tag, data)
                                .background(R.id.bt_tag, getResources().getDrawable(R.drawable.shape_17_tag_selected));
                    }
                }).attachTo(recyclerView).updateData(strings);
            }
        }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
        ModelRx2DataSource<TeacherListBean> dataSource = new ModelRx2DataSource<>(new ModelRx2DataSource.OnLoadSource() {
            @Override
            public Flowable<List> loadSource(int page, Rx2DataSource.DoneActionRegister register) {
                pageNum = page == 1 ? 0 : pageNum;
                return OkGo.<HttpResultRows<TeacherListBean>>post(HttpConfig.BASE_URL + HttpConfig.SELECT_TEACHER)
                        .params("page", pageNum)
                        .params("ResearchField", "")
                        .params("type", "app")
                        .params("para", para)
                        .converter(new JsonConverter<HttpResultRows<TeacherListBean>>() {
                            @Override
                            public void test() {
                                super.test();
                            }
                        }).adapt(new FlowableBody<>())
                        .flatMap(listResultBean -> {
                            pageNum = listResultBean.getItems().getPage();
                            if (listResultBean.getStatus() == 200) {
                                return Flowable.just(listResultBean.getItems() == null ? new ArrayList<TeacherListBean>() : listResultBean.getItems().getRows());
                            } else {
                                ToastUtil.showShort(listResultBean.getMsg());
                                return Flowable.just(new ArrayList<>());
                            }
                        });

            }
        });
        slimAdapterEx.updateData(new ArrayList<>());
        MVCCoolHelper<List<TeacherListBean>> mvcHelper = new MVCCoolHelper<>(dataBinding.coolRefreshView);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapterEx);
        dataBinding.floatingSearchView.setOnSearchListener(new FloatingSearchView.OnSearchListener() {
            @Override
            public void onSuggestionClicked(SearchSuggestion searchSuggestion) {

            }

            @Override
            public void onSearchAction(String currentQuery) {

                if (!TextUtils.isEmpty(currentQuery)) {
                    para = currentQuery;
                    mvcHelper.refresh();
                }
            }
        });
        ItemClickSupport.addTo(dataBinding.recyclerView)
                .setOnItemClickListener((recyclerView, position, v) -> {
                    Intent intent = new Intent();
                    intent.putExtra(Config.BEAN,slimAdapterEx.getDataItem(position));
                    setResult(Config.SUCCESS,intent);
                    finish();
                });
    }

    @Override
    public void setView() {

    }
}
